package com.moyun.finance.controller;

import com.moyun.finance.entity.OrgUser;
import com.moyun.finance.entity.Response;
import com.moyun.finance.service.OrgUserService;
import com.moyun.finance.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/org/user")
public class OrgUserController {
    @Autowired
    OrgUserService ous;

    //用户注册、添加
    @RequestMapping("/save")
    public @ResponseBody Response save(@RequestBody OrgUser user){
        Response res = ous.save(user);
        return res;
    }

    //用户登录
    @RequestMapping("/login")
    public @ResponseBody Response login(String userid, String userPwd, HttpServletRequest request){
        Response res = ous.login(userid, userPwd, request);
        return res;
    }

    //用户停用-启用
    @RequestMapping("/stopOpen")
    public @ResponseBody Response stopUser(String userid, String delState){
        Response res = ous.stopUser(userid, Integer.parseInt(delState));
        return res;
    }

//    //用户启用
//    @RequestMapping("/open")
//    public @ResponseBody Response openUser(String userid){
//        Response res = ous.openUser(userid);
//        return res;
//    }

    //修改用户资料
    @RequestMapping("/update")
    public @ResponseBody Response update(@RequestBody OrgUser user){
        Response res = ous.updateUser(user);
        return res;
    }

    //查询指定公司所有用户
    @RequestMapping("/findAllByCompanyId")
    public @ResponseBody Response findAllByDeptId(String companyId){
        List<OrgUser> users = ous.findAll(companyId);
        return new Response("",users,true);
    }

    //获取当前用户
    @RequestMapping("/getLoginUser")
    public @ResponseBody Response getLoginUser(HttpServletRequest request){
        return new Response("",UserUtil.getLoginUser(request),true);
    }

    //用户退出
    @RequestMapping("/getOutUser")
    public @ResponseBody Response getOutUser(HttpServletRequest request){
        request.getSession().invalidate();
        return new Response("退出登录!",null,true);
    }

    //根据用户姓名 模糊查询用户
    @RequestMapping("/getUserByName")
    public @ResponseBody Response getUserByName(String userName, String companyId){
        List<OrgUser> users = ous.findUserByName(userName,companyId);
        return new Response("",users,true);
    }

    //根据id查用户
    @RequestMapping("/findUserById")
    public @ResponseBody Response findUserById(String userId){
        OrgUser user = ous.findUserById(userId);
        return new Response("",user,true);
    }

}
